package com.gnerv.battle.boot.module.base.service.impl;

import com.gnerv.battle.boot.module.base.model.bo.ViewBO;
import com.gnerv.battle.boot.module.base.model.dto.ViewDTO;
import com.gnerv.battle.boot.module.base.model.dto.ViewTreeDTO;
import com.gnerv.battle.boot.module.base.model.entity.View;
import com.gnerv.battle.boot.module.base.repository.mapper.RoleViewMapper;
import com.gnerv.battle.boot.module.base.repository.mapper.ViewMapper;
import com.gnerv.battle.boot.module.base.service.CurrentAccountService;
import com.gnerv.battle.boot.module.base.service.ViewService;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author Gnerv LiGen
 */
@Service
@AllArgsConstructor
public class ViewServiceImpl implements ViewService {

    private final ViewMapper viewMapper;
    private final RoleViewMapper roleViewMapper;
    private final CurrentAccountService currentAccountService;

    @Override
    public boolean create(@NonNull ViewBO viewBO) {
        View view = viewBO.toCreateView();
        view.setCreator(currentAccountService.accountUkId());
        viewMapper.insert(view);
        return true;
    }

    @Override
    public boolean deleteByUkId(@NonNull String ukId) {
        viewMapper.deleteByUkId(ukId);
        return true;
    }

    @Override
    public boolean updateByUkId(@NonNull ViewBO viewBO) {
        View view = viewBO.toUpdateView();
        viewMapper.updateByUkId(view);
        return true;
    }

    @Override
    public ViewDTO selectByUkId(@NonNull String ukId) {
        return viewMapper.selectByUkId(ukId);
    }

    @Override
    public List<ViewDTO> listByName(String name) {
        return viewMapper.listByName(name);
    }

    @Override
    public List<ViewDTO> listByParentUkId(@NonNull String parentUkId) {
        return viewMapper.listByParentUkId(parentUkId);
    }

    @Override
    public List<ViewTreeDTO> listTree(String parentUkId) {
        return viewMapper.listTree(parentUkId);
    }

    @Override
    public boolean bindRole(@NonNull String viewUkId, @NonNull Set<String> roleUkIdList) {
        roleViewMapper.insertViewRole(viewUkId, roleUkIdList, currentAccountService.accountUkId());
        return true;
    }

    @Override
    public boolean unbindRole(@NonNull String viewUkId) {
        roleViewMapper.deleteByViewUkId(viewUkId);
        return true;
    }
}




